package com.midian.service.device.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.midian.common.dao.BaseDaoBySql;
import com.midian.common.dao.BaseDaoTemplet;
import com.midian.common.dao.OrderHelper;
import com.midian.common.dao.SearchHelper;
import com.midian.common.dao.SearchHelper.Type;
import com.midian.common.exception.BizException;
import com.midian.common.pager.Pager;
import com.midian.common.system.SysStatic;
import com.midian.model.device.Device;
import com.midian.service.common.BaseService;
import com.midian.service.device.IDeviceService;

/**
 * 设备号表业务实现类
 * @author Mengfh
 *
 */
@Service("deviceService")
public class DeviceServiceImpl extends BaseService implements IDeviceService {
	/**设备号表DAO*/
	private BaseDaoBySql<Device> deviceDAO;
	
	@Resource(name = "deviceDAO")
	public void setDeviceDAO(BaseDaoBySql<Device> deviceDAO) {
		this.deviceDAO = deviceDAO;
	}

	@Override
	public Device add(Device target) throws BizException {
		this.deviceDAO.add(target);
		return target;
	}

	@Override
	public void update(Device target) throws BizException {
		this.deviceDAO.updateByConvertHql(target);
	}

	@Override
	public void delete(Serializable id) throws BizException {
		this.deviceDAO.delete(id);
	}

	@Override
	public void delete(Serializable[] ids) throws BizException {
		this.deviceDAO.delete(ids);
	}

	@Override
	public Device get(Serializable id) throws BizException {
		return this.deviceDAO.get(id);
	}

	@Override
	public List<Device> showList(Device target, Pager pager, List<OrderHelper> helper) throws BizException {
		return this.deviceDAO.find(target, getSearchHelper(), helper, pager);
	}

	@Override
	public void opDelFlag(Serializable[] ids, Integer status) throws BizException {
		this.deviceDAO.opDelFlag(ids, status);
	}

	@Override
	protected List<SearchHelper> getSearchHelper() {
		String[] attrs = {};
		Type[] types = {};
		return SearchHelper.getSearchList(attrs, types);
	}
	
	
	/**
	 * 保存或更新设备号
	 * @param device 设备号对象
	 * @throws BizException
	 * @return 返回设备号对象
	 */
	@Override
	public Device saveOrUpdateDevice(Device device) throws BizException {
		String hql = "";
		//根据用户id查找用户的设备号对象
		hql = "from Device o where o.userId = ?";
		Device targetDevice = (Device) this.deviceDAO.findObject(hql, new Object[]{device.getUserId()});
		if(targetDevice != null) {
			//更新用户的设备号
			hql="update device o set o.updateTime = ?,o.frontType = ?,o.isReceive = (case when o.isReceive is not null then o.isReceive else 1 end),o.deviceToken = ? where o.userId = ?";
			this.deviceDAO.updateSQL(hql, new Object[]{new Date(), device.getFrontType(), device.getDeviceToken(), device.getUserId()});
		} else {
			//设置接收通知的初始值为1
			device.setIsReceive(1);
			//添加用户的设备号
			add(device);
			targetDevice = device;
		}
		//清空除当前用户外的其他用户之前保存过的设备号
		hql = "update Device o set o.deviceToken = null where o.deviceToken = ? and o.userId != ?";
		this.deviceDAO.update(hql, new Object[]{device.getDeviceToken(),device.getUserId()});
		//更新设备表的前端类型
		this.updateFrontType(device.getUserId(), device.getFrontType());
		return targetDevice;
	}

	
	/**
	 * 通过会员Id获取设备
	 * @param userId
	 * @return
	 */
	@Override
	public Device getDeviceByUserId(String userId) {
		String hql="from Device o where o.userId = ? and o.isReceive = ? and o.deviceToken is not null";		
		return (Device) this.deviceDAO.findObject(hql, new Object[]{userId, SysStatic.NORMALDELFLAG});
	}
	/**
	 * 根据用户id取出所有device无论isreceive值是多少
	 * @param userid
	 * @return
	 */
	@Override
	public Device getAllDeviceByUserId(String userId){
		String hql = "from Device o where o.userId = ?and o.deviceToken is not null";
		return (Device) this.deviceDAO.findObject(hql, new Object[]{userId});
	}
	/**
	 * 更改用户的推送接收状态
	 * @param userId 用户id
	 * @param receiveStatus 推送接收状态
	 * @throws BizException
	 */
	@Override
	public void updateUserPushReceiveStatus(String userId, Integer receiveStatus) throws BizException {
		String hql = "update Device o set o.isReceive = ? where o.userId = ? and o.delFlag = ?";
		this.deviceDAO.update(hql, new Object[]{receiveStatus, userId, SysStatic.NORMALDELFLAG});
	}
	
	/**
	 * 清空该设备号的记录
	 * @param deviceToken 设备号
	 * @throws BizException
	 */
	@Override
	public void clearByDeviceToken(String deviceToken) throws BizException {
		String hql = "update Device o set o.deviceToken = null where o.deviceToken = ?";
		this.deviceDAO.update(hql, new Object[]{deviceToken});
	}

	
	/**
	 * 更新前端类型
	 * @param userId 用户id
	 * @param frontType 前端类型 
	 */
	@Override
	public void updateFrontType(String userId, String frontType) throws BizException {
		String hql = "update Device o set o.frontType = ? where o.userId = ?";
		this.deviceDAO.update(hql, new Object[]{frontType, userId});
	}
}
